package com.qdlc.p2p.dal.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.dao.UserCertificationApplyDao;
import com.qdlc.p2p.dal.dto.CertificationType;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserCertificationApply;

@Repository("userCertificationApplyDao")
public class UserCertificationApplyDaoImpl extends BaseDaoImpl<UserCertificationApply> implements UserCertificationApplyDao {

	@Override
	public int getStatusByUserAndTypeId(User user, long typeId) {
		int status;
		String sql = "select status from UserCertificationApply where user=? and type.id=? ";
		Query query = em.createQuery(sql);
		query.setParameter(1, user);
		query.setParameter(2, typeId);
		query.setMaxResults(1);
		try {			
			status = Integer.parseInt(query.getSingleResult().toString());
		} catch (Exception e) {
			status = -1;
		}
		return status;
	}

	@SuppressWarnings("unchecked")
	@Override
	public UserCertificationApply getUserCertificationApplyByUser(User user,CertificationType type) {
		String sql = "from UserCertificationApply where user=? and type=?";
		Query query = em.createQuery(sql);
        query.setParameter(1, user);
        query.setParameter(2, type);
        List<UserCertificationApply> list = query.getResultList();
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
	}

}
